<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>3.2 输入 | 引言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../c03/s03.html" />
    
    
    <link rel="prev" href="../c03/s01.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.2"
        data-chapter-title="3.2 输入"
        data-filepath="c03/s02.md"
        data-basepath=".."
        data-revision="Fri Feb 10 2017 17:36:30 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        引言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="c01/index.html">
            
                
                    <a href="../c01/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        1 关于Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="c01/s01.html">
            
                
                    <a href="../c01/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        1.1 Tornado是为何物
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="c01/s02.html">
            
                
                    <a href="../c01/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        1.2 Tornado与Django
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="c02/index.html">
            
                
                    <a href="../c02/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        2 初识Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="c02/s01.html">
            
                
                    <a href="../c02/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        2.1 安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="c02/s02.html">
            
                
                    <a href="../c02/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        2.2 Hello Itcast
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="c02/s03.html">
            
                
                    <a href="../c02/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        2.3 httpserver
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="c02/s04.html">
            
                
                    <a href="../c02/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        2.4 options
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="c02/s05.html">
            
                
                    <a href="../c02/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        2.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="c03/index.html">
            
                
                    <a href="../c03/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        3 深入Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="c03/s01.html">
            
                
                    <a href="../c03/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        3.1 Application
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.2" data-path="c03/s02.html">
            
                
                    <a href="../c03/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        3.2 输入
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="c03/s03.html">
            
                
                    <a href="../c03/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        3.3 输出
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="c03/s04.html">
            
                
                    <a href="../c03/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        3.4 接口与调用顺序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="c03/s05.html">
            
                
                    <a href="../c03/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        3.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="c04/index.html">
            
                
                    <a href="../c04/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        4 模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="c04/s01.html">
            
                
                    <a href="../c04/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        4.1 静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="c04/s02.html">
            
                
                    <a href="../c04/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        4.2 使用模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="c04/s03.html">
            
                
                    <a href="../c04/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        4.3 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="c05/index.html">
            
                
                    <a href="../c05/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        5 数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="c05/s01.html">
            
                
                    <a href="../c05/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        5.1 数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="c05/s02.html">
            
                
                    <a href="../c05/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        5.2 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="c06/index.html">
            
                
                    <a href="../c06/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        6 安全应用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="c06/s01.html">
            
                
                    <a href="../c06/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        6.1 Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="c06/s02.html">
            
                
                    <a href="../c06/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        6.2 XSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="c06/s03.html">
            
                
                    <a href="../c06/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        6.3 用户验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="c06/s04.html">
            
                
                    <a href="../c06/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        6.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="c07/index.html">
            
                
                    <a href="../c07/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        7 异步与WebSocket
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="c07/s01.html">
            
                
                    <a href="../c07/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        7.1 认识异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="c07/s02.html">
            
                
                    <a href="../c07/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        7.2 Tornado异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="c07/s03.html">
            
                
                    <a href="../c07/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        7.3 WebSocket
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="c07/s04.html">
            
                
                    <a href="../c07/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        7.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="c08/index.html">
            
                
                    <a href="../c08/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        8 部署
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="c08/s01.html">
            
                
                    <a href="../c08/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        8.1 部署Tornado
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >引言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="32-&#x8F93;&#x5165;">3.2 &#x8F93;&#x5165;</h1>
<p>&#x4E0B;&#x9762;&#x51E0;&#x8282;&#x4E3B;&#x8981;&#x8BB2;&#x89E3;tornado.web.RequestHandler&#x3002;</p>
<p>&#x56DE;&#x60F3;&#x4E00;&#x4E0B;&#xFF0C;&#x5229;&#x7528;HTTP&#x534F;&#x8BAE;&#x5411;&#x670D;&#x52A1;&#x5668;&#x4F20;&#x53C2;&#x6709;&#x51E0;&#x79CD;&#x9014;&#x5F84;&#xFF1F;</p>
<ul>
<li>&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#xFF08;query string)&#xFF0C;&#x5F62;&#x5982;key1=value1&amp;key2=value2&#xFF1B;</li>
<li>&#x8BF7;&#x6C42;&#x4F53;&#xFF08;body&#xFF09;&#x4E2D;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x6BD4;&#x5982;&#x8868;&#x5355;&#x6570;&#x636E;&#x3001;json&#x3001;xml&#xFF1B;</li>
<li>&#x63D0;&#x53D6;uri&#x7684;&#x7279;&#x5B9A;&#x90E8;&#x5206;&#xFF0C;&#x5982;/blogs/2016/09/0001&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x7684;&#x8DEF;&#x7531;&#x4E2D;&#x7528;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x622A;&#x53D6;&#xFF1B;</li>
<li>&#x5728;http&#x62A5;&#x6587;&#x7684;&#x5934;&#xFF08;header&#xFF09;&#x4E2D;&#x589E;&#x52A0;&#x81EA;&#x5B9A;&#x4E49;&#x5B57;&#x6BB5;&#xFF0C;&#x5982;X-XSRFToken=itcast&#x3002;</li>
</ul>
<p>&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x6765;&#x770B;&#x4E0B;tornado&#x4E2D;&#x4E3A;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86;&#x54EA;&#x4E9B;&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x7684;&#x4FE1;&#x606F;&#x3002;</p>
<h2 id="1-&#x83B7;&#x53D6;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x53C2;&#x6570;">1. &#x83B7;&#x53D6;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x53C2;&#x6570;</h2>
<h4 id="getqueryargumentname-defaultargdefault-striptrue">get_query_argument(name, default=_ARG_DEFAULT, strip=True)</h4>
<p>&#x4ECE;&#x8BF7;&#x6C42;&#x7684;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x53C2;&#x6570;name&#x7684;&#x503C;&#xFF0C;&#x5982;&#x679C;&#x51FA;&#x73B0;&#x591A;&#x4E2A;&#x540C;&#x540D;&#x53C2;&#x6570;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x7684;&#x503C;&#x3002;</p>
<p>default&#x4E3A;&#x8BBE;&#x503C;&#x672A;&#x4F20;name&#x53C2;&#x6570;&#x65F6;&#x8FD4;&#x56DE;&#x7684;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x5982;&#x82E5;default&#x4E5F;&#x672A;&#x8BBE;&#x7F6E;&#xFF0C;&#x5219;&#x4F1A;&#x629B;&#x51FA;tornado.web.MissingArgumentError&#x5F02;&#x5E38;&#x3002;</p>
<p>strip&#x8868;&#x793A;&#x662F;&#x5426;&#x8FC7;&#x6EE4;&#x6389;&#x5DE6;&#x53F3;&#x4E24;&#x8FB9;&#x7684;&#x7A7A;&#x767D;&#x5B57;&#x7B26;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;&#x8FC7;&#x6EE4;&#x3002;</p>
<h4 id="getqueryargumentsname-striptrue">get_query_arguments(name, strip=True)</h4>
<p>&#x4ECE;&#x8BF7;&#x6C42;&#x7684;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x53C2;&#x6570;name&#x7684;&#x503C;&#xFF0C;&#x6CE8;&#x610F;&#x8FD4;&#x56DE;&#x7684;&#x662F;list&#x5217;&#x8868;&#xFF08;&#x5373;&#x4F7F;&#x5BF9;&#x5E94;name&#x53C2;&#x6570;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x503C;&#xFF09;&#x3002;&#x82E5;&#x672A;&#x627E;&#x5230;name&#x53C2;&#x6570;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x7A7A;&#x5217;&#x8868;[]&#x3002;</p>
<p>strip&#x540C;&#x524D;&#xFF0C;&#x4E0D;&#x518D;&#x8D58;&#x8FF0;&#x3002;</p>
<h2 id="2-&#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x4F53;&#x53C2;&#x6570;">2. &#x83B7;&#x53D6;&#x8BF7;&#x6C42;&#x4F53;&#x53C2;&#x6570;</h2>
<h4 id="getbodyargumentname-defaultargdefault-striptrue">get_body_argument(name, default=_ARG_DEFAULT, strip=True)</h4>
<p>&#x4ECE;&#x8BF7;&#x6C42;&#x4F53;&#x4E2D;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x53C2;&#x6570;name&#x7684;&#x503C;&#xFF0C;&#x5982;&#x679C;&#x51FA;&#x73B0;&#x591A;&#x4E2A;&#x540C;&#x540D;&#x53C2;&#x6570;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x7684;&#x503C;&#x3002;</p>
<p>default&#x4E0E;strip&#x540C;&#x524D;&#xFF0C;&#x4E0D;&#x518D;&#x8D58;&#x8FF0;&#x3002;</p>
<h4 id="getbodyargumentsname-striptrue">get_body_arguments(name, strip=True)</h4>
<p>&#x4ECE;&#x8BF7;&#x6C42;&#x4F53;&#x4E2D;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x53C2;&#x6570;name&#x7684;&#x503C;&#xFF0C;&#x6CE8;&#x610F;&#x8FD4;&#x56DE;&#x7684;&#x662F;list&#x5217;&#x8868;&#xFF08;&#x5373;&#x4F7F;&#x5BF9;&#x5E94;name&#x53C2;&#x6570;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x503C;&#xFF09;&#x3002;&#x82E5;&#x672A;&#x627E;&#x5230;name&#x53C2;&#x6570;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x7A7A;&#x5217;&#x8868;[]&#x3002;</p>
<p>strip&#x540C;&#x524D;&#xFF0C;&#x4E0D;&#x518D;&#x8D58;&#x8FF0;&#x3002;</p>
<h4 id="&#x8BF4;&#x660E;">&#x8BF4;&#x660E;</h4>
<p>&#x5BF9;&#x4E8E;&#x8BF7;&#x6C42;&#x4F53;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x8981;&#x6C42;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4E14;&#x683C;&#x5F0F;&#x4E3A;&#x8868;&#x5355;&#x7F16;&#x7801;&#x683C;&#x5F0F;&#xFF08;&#x4E0E;url&#x4E2D;&#x7684;&#x8BF7;&#x6C42;&#x5B57;&#x7B26;&#x4E32;&#x683C;&#x5F0F;&#x76F8;&#x540C;&#xFF09;&#xFF0C;&#x5373;key1=value1&amp;key2=value2&#xFF0C;HTTP&#x62A5;&#x6587;&#x5934;Header&#x4E2D;&#x7684;&quot;Content-Type&quot;&#x4E3A;application/x-www-form-urlencoded &#x6216; multipart/form-data&#x3002;&#x5BF9;&#x4E8E;&#x8BF7;&#x6C42;&#x4F53;&#x6570;&#x636E;&#x4E3A;json&#x6216;xml&#x7684;&#xFF0C;&#x65E0;&#x6CD5;&#x901A;&#x8FC7;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x83B7;&#x53D6;&#x3002;</p>
<h2 id="3-&#x524D;&#x4E24;&#x7C7B;&#x65B9;&#x6CD5;&#x7684;&#x6574;&#x5408;">3. &#x524D;&#x4E24;&#x7C7B;&#x65B9;&#x6CD5;&#x7684;&#x6574;&#x5408;</h2>
<h4 id="getargumentname-defaultargdefault-striptrue">get_argument(name, default=_ARG_DEFAULT, strip=True)</h4>
<p>&#x4ECE;&#x8BF7;&#x6C42;&#x4F53;&#x548C;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x53C2;&#x6570;name&#x7684;&#x503C;&#xFF0C;&#x5982;&#x679C;&#x51FA;&#x73B0;&#x591A;&#x4E2A;&#x540C;&#x540D;&#x53C2;&#x6570;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x7684;&#x503C;&#x3002;</p>
<p>default&#x4E0E;strip&#x540C;&#x524D;&#xFF0C;&#x4E0D;&#x518D;&#x8D58;&#x8FF0;&#x3002;</p>
<h4 id="getargumentsname-striptrue">get_arguments(name, strip=True)</h4>
<p>&#x4ECE;&#x8BF7;&#x6C42;&#x4F53;&#x548C;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x53C2;&#x6570;name&#x7684;&#x503C;&#xFF0C;&#x6CE8;&#x610F;&#x8FD4;&#x56DE;&#x7684;&#x662F;list&#x5217;&#x8868;&#xFF08;&#x5373;&#x4F7F;&#x5BF9;&#x5E94;name&#x53C2;&#x6570;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x503C;&#xFF09;&#x3002;&#x82E5;&#x672A;&#x627E;&#x5230;name&#x53C2;&#x6570;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x7A7A;&#x5217;&#x8868;[]&#x3002;</p>
<p>strip&#x540C;&#x524D;&#xFF0C;&#x4E0D;&#x518D;&#x8D58;&#x8FF0;&#x3002;</p>
<h4 id="&#x8BF4;&#x660E;">&#x8BF4;&#x660E;</h4>
<p>&#x5BF9;&#x4E8E;&#x8BF7;&#x6C42;&#x4F53;&#x4E2D;&#x6570;&#x636E;&#x7684;&#x8981;&#x6C42;&#x540C;&#x524D;&#x3002; <strong>&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x6700;&#x5E38;&#x7528;</strong>&#x3002;</p>
<p>&#x7528;&#x4EE3;&#x7801;&#x6765;&#x770B;&#x4E0A;&#x8FF0;&#x516D;&#x4E2D;&#x65B9;&#x6CD5;&#x7684;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># coding:utf-8</span>

<span class="hljs-keyword">import</span> tornado.web
<span class="hljs-keyword">import</span> tornado.ioloop
<span class="hljs-keyword">import</span> tornado.httpserver
<span class="hljs-keyword">import</span> tornado.options
<span class="hljs-keyword">from</span> tornado.options <span class="hljs-keyword">import</span> options, define
<span class="hljs-keyword">from</span> tornado.web <span class="hljs-keyword">import</span> RequestHandler, MissingArgumentError

define(<span class="hljs-string">&quot;port&quot;</span>, default=<span class="hljs-number">8000</span>, type=int, help=<span class="hljs-string">&quot;run server on the given port.&quot;</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self)</span>:</span>
        query_arg = self.get_query_argument(<span class="hljs-string">&quot;a&quot;</span>)
        query_args = self.get_query_arguments(<span class="hljs-string">&quot;a&quot;</span>)
        body_arg = self.get_body_argument(<span class="hljs-string">&quot;a&quot;</span>)
        body_args = self.get_body_arguments(<span class="hljs-string">&quot;a&quot;</span>, strip=<span class="hljs-keyword">False</span>)
        arg = self.get_argument(<span class="hljs-string">&quot;a&quot;</span>)
        args = self.get_arguments(<span class="hljs-string">&quot;a&quot;</span>)

        default_arg = self.get_argument(<span class="hljs-string">&quot;b&quot;</span>, <span class="hljs-string">&quot;itcast&quot;</span>)
        default_args = self.get_arguments(<span class="hljs-string">&quot;b&quot;</span>)

        <span class="hljs-keyword">try</span>:
            missing_arg = self.get_argument(<span class="hljs-string">&quot;c&quot;</span>)
        <span class="hljs-keyword">except</span> MissingArgumentError <span class="hljs-keyword">as</span> e:
            missing_arg = <span class="hljs-string">&quot;We catched the MissingArgumentError!&quot;</span>
            <span class="hljs-keyword">print</span> e
        missing_args = self.get_arguments(<span class="hljs-string">&quot;c&quot;</span>)

        rep = <span class="hljs-string">&quot;query_arg:%s&lt;br/&gt;&quot;</span> % query_arg
        rep += <span class="hljs-string">&quot;query_args:%s&lt;br/&gt;&quot;</span> % query_args 
        rep += <span class="hljs-string">&quot;body_arg:%s&lt;br/&gt;&quot;</span>  % body_arg
        rep += <span class="hljs-string">&quot;body_args:%s&lt;br/&gt;&quot;</span> % body_args
        rep += <span class="hljs-string">&quot;arg:%s&lt;br/&gt;&quot;</span>  % arg
        rep += <span class="hljs-string">&quot;args:%s&lt;br/&gt;&quot;</span> % args 
        rep += <span class="hljs-string">&quot;default_arg:%s&lt;br/&gt;&quot;</span> % default_arg 
        rep += <span class="hljs-string">&quot;default_args:%s&lt;br/&gt;&quot;</span> % default_args 
        rep += <span class="hljs-string">&quot;missing_arg:%s&lt;br/&gt;&quot;</span> % missing_arg
        rep += <span class="hljs-string">&quot;missing_args:%s&lt;br/&gt;&quot;</span> % missing_args

        self.write(rep)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
    tornado.options.parse_command_line()
    app = tornado.web.Application([
        (<span class="hljs-string">r&quot;/&quot;</span>, IndexHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()
</code></pre>
<p><img src="../images/get_argument_demo.png" alt="&#x6F14;&#x793A;&#x7ED3;&#x679C;"></p>
<h4 id="&#x6CE8;&#x610F;&#xFF1A;&#x4EE5;&#x4E0A;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x7684;&#x90FD;&#x662F;unicode&#x5B57;&#x7B26;&#x4E32;">&#x6CE8;&#x610F;&#xFF1A;&#x4EE5;&#x4E0A;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x7684;&#x90FD;&#x662F;unicode&#x5B57;&#x7B26;&#x4E32;</h4>
<h2 id="&#x601D;&#x8003;">&#x601D;&#x8003;</h2>
<ol>
<li>&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x8BBE;&#x7F6E;default&#xFF0C;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x4E0D;&#x8BBE;&#x7F6E;default&#xFF1F;</li>
<li>default&#x7684;&#x9ED8;&#x8BA4;&#x503C;_ARG_DEFAULT&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</li>
<li>&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x4F7F;&#x7528;strip&#xFF0C;&#x4EA6;&#x5373;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x8981;&#x622A;&#x65AD;&#x7A7A;&#x767D;&#x5B57;&#x7B26;&#xFF0C;&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x4E0D;&#x9700;&#x8981;&#xFF1F;</li>
</ol>
<h2 id="4-&#x5173;&#x4E8E;&#x8BF7;&#x6C42;&#x7684;&#x5176;&#x4ED6;&#x4FE1;&#x606F;">4. &#x5173;&#x4E8E;&#x8BF7;&#x6C42;&#x7684;&#x5176;&#x4ED6;&#x4FE1;&#x606F;</h2>
<p>RequestHandler.request &#x5BF9;&#x8C61;&#x5B58;&#x50A8;&#x4E86;&#x5173;&#x4E8E;&#x8BF7;&#x6C42;&#x7684;&#x76F8;&#x5173;&#x4FE1;&#x606F;&#xFF0C;&#x5177;&#x4F53;&#x5C5E;&#x6027;&#x6709;&#xFF1A;</p>
<ul>
<li>method HTTP&#x7684;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#xFF0C;&#x5982;GET&#x6216;POST;</li>
<li>host &#x88AB;&#x8BF7;&#x6C42;&#x7684;&#x4E3B;&#x673A;&#x540D;&#xFF1B;</li>
<li>uri &#x8BF7;&#x6C42;&#x7684;&#x5B8C;&#x6574;&#x8D44;&#x6E90;&#x6807;&#x793A;&#xFF0C;&#x5305;&#x62EC;&#x8DEF;&#x5F84;&#x548C;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#xFF1B;</li>
<li>path &#x8BF7;&#x6C42;&#x7684;&#x8DEF;&#x5F84;&#x90E8;&#x5206;&#xFF1B;</li>
<li>query &#x8BF7;&#x6C42;&#x7684;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x90E8;&#x5206;&#xFF1B;</li>
<li>version &#x4F7F;&#x7528;&#x7684;HTTP&#x7248;&#x672C;&#xFF1B;</li>
<li><strong>headers</strong> &#x8BF7;&#x6C42;&#x7684;&#x534F;&#x8BAE;&#x5934;&#xFF0C;&#x662F;&#x7C7B;&#x5B57;&#x5178;&#x578B;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x652F;&#x6301;&#x5173;&#x952E;&#x5B57;&#x7D22;&#x5F15;&#x7684;&#x65B9;&#x5F0F;&#x83B7;&#x53D6;&#x7279;&#x5B9A;&#x534F;&#x8BAE;&#x5934;&#x4FE1;&#x606F;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;request.headers[&quot;Content-Type&quot;]</li>
<li><strong>body</strong> &#x8BF7;&#x6C42;&#x4F53;&#x6570;&#x636E;&#xFF1B;</li>
<li><strong>remote_ip</strong> &#x5BA2;&#x6237;&#x7AEF;&#x7684;IP&#x5730;&#x5740;&#xFF1B;</li>
<li><strong>files</strong> &#x7528;&#x6237;&#x4E0A;&#x4F20;&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x4E3A;&#x5B57;&#x5178;&#x7C7B;&#x578B;&#xFF0C;&#x578B;&#x5982;&#xFF1A;<pre><code class="lang-python">{
  <span class="hljs-string">&quot;form_filename1&quot;</span>:[&lt;tornado.httputil.HTTPFile&gt;, &lt;tornado.httputil.HTTPFile&gt;],
  <span class="hljs-string">&quot;form_filename2&quot;</span>:[&lt;tornado.httputil.HTTPFile&gt;,],
  ... 
}
</code></pre>
tornado.httputil.HTTPFile&#x662F;&#x63A5;&#x6536;&#x5230;&#x7684;&#x6587;&#x4EF6;&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x6709;&#x4E09;&#x4E2A;&#x5C5E;&#x6027;&#xFF1A;<ul>
<li>filename &#x6587;&#x4EF6;&#x7684;&#x5B9E;&#x9645;&#x540D;&#x5B57;&#xFF0C;&#x4E0E;form_filename1&#x4E0D;&#x540C;&#xFF0C;&#x5B57;&#x5178;&#x4E2D;&#x7684;&#x952E;&#x540D;&#x4EE3;&#x8868;&#x7684;&#x662F;&#x8868;&#x5355;&#x5BF9;&#x5E94;&#x9879;&#x7684;&#x540D;&#x5B57;&#xFF1B;</li>
<li>body &#x6587;&#x4EF6;&#x7684;&#x6570;&#x636E;&#x5B9E;&#x4F53;&#xFF1B;</li>
<li>content_type &#x6587;&#x4EF6;&#x7684;&#x7C7B;&#x578B;&#x3002;
&#x8FD9;&#x4E09;&#x4E2A;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x50CF;&#x5B57;&#x5178;&#x4E00;&#x6837;&#x652F;&#x6301;&#x5173;&#x952E;&#x5B57;&#x7D22;&#x5F15;&#xFF0C;&#x5982;request.files[&quot;form_filename1&quot;][0][&quot;body&quot;]&#x3002;</li>
</ul>
</li>
</ul>
<p>&#x6211;&#x4EEC;&#x6765;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x4E0A;&#x4F20;&#x6587;&#x4EF6;&#x5E76;&#x4FDD;&#x5B58;&#x5728;&#x670D;&#x52A1;&#x5668;&#x672C;&#x5730;&#x7684;&#x5C0F;&#x7A0B;&#x5E8F;upload.py&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># coding:utf-8</span>

<span class="hljs-keyword">import</span> tornado.web
<span class="hljs-keyword">import</span> tornado.ioloop
<span class="hljs-keyword">import</span> tornado.httpserver
<span class="hljs-keyword">import</span> tornado.options
<span class="hljs-keyword">from</span> tornado.options <span class="hljs-keyword">import</span> options, define
<span class="hljs-keyword">from</span> tornado.web <span class="hljs-keyword">import</span> RequestHandler

define(<span class="hljs-string">&quot;port&quot;</span>, default=<span class="hljs-number">8000</span>, type=int, help=<span class="hljs-string">&quot;run server on the given port.&quot;</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(<span class="hljs-string">&quot;hello itcast.&quot;</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UploadHandler</span><span class="hljs-params">(RequestHandler)</span>:</span> 
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self)</span>:</span>
        files = self.request.files
        img_files = files.get(<span class="hljs-string">&apos;img&apos;</span>)
        <span class="hljs-keyword">if</span> img_files:
            img_file = img_files[<span class="hljs-number">0</span>][<span class="hljs-string">&quot;body&quot;</span>]
            file = open(<span class="hljs-string">&quot;./itcast&quot;</span>, <span class="hljs-string">&apos;w+&apos;</span>)
            file.write(img_file)
            file.close()
        self.write(<span class="hljs-string">&quot;OK&quot;</span>)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
    tornado.options.parse_command_line()
    app = tornado.web.Application([
        (<span class="hljs-string">r&quot;/&quot;</span>, IndexHandler),
        (<span class="hljs-string">r&quot;/upload&quot;</span>, UploadHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()
</code></pre>
<p><img src="../images/img_upload_server.png" alt="&#x4E0A;&#x4F20;&#x56FE;&#x7247;&#x670D;&#x52A1;&#x5668;&#x7AEF;">
<img src="../images/img_upload_fe.png" alt="&#x4E0A;&#x4F20;&#x56FE;&#x7247;&#x524D;&#x7AEF;"></p>
<h2 id="5-&#x6B63;&#x5219;&#x63D0;&#x53D6;uri">5. &#x6B63;&#x5219;&#x63D0;&#x53D6;uri</h2>
<p>tornado&#x4E2D;&#x5BF9;&#x4E8E;&#x8DEF;&#x7531;&#x6620;&#x5C04;&#x4E5F;&#x652F;&#x6301;&#x6B63;&#x5219;&#x63D0;&#x53D6;uri&#xFF0C;&#x63D0;&#x53D6;&#x51FA;&#x6765;&#x7684;&#x53C2;&#x6570;&#x4F1A;&#x4F5C;&#x4E3A;RequestHandler&#x4E2D;&#x5BF9;&#x5E94;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x7684;&#x6210;&#x5458;&#x65B9;&#x6CD5;&#x53C2;&#x6570;&#x3002;&#x82E5;&#x5728;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x5B9A;&#x4E49;&#x4E86;&#x540D;&#x5B57;&#xFF0C;&#x5219;&#x53C2;&#x6570;&#x6309;&#x540D;&#x4F20;&#x9012;&#xFF1B;&#x82E5;&#x672A;&#x5B9A;&#x4E49;&#x540D;&#x5B57;&#xFF0C;&#x5219;&#x53C2;&#x6570;&#x6309;&#x987A;&#x5E8F;&#x4F20;&#x9012;&#x3002;&#x63D0;&#x53D6;&#x51FA;&#x6765;&#x7684;&#x53C2;&#x6570;&#x4F1A;&#x4F5C;&#x4E3A;&#x5BF9;&#x5E94;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&#x7684;&#x6210;&#x5458;&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># coding:utf-8</span>

<span class="hljs-keyword">import</span> tornado.web
<span class="hljs-keyword">import</span> tornado.ioloop
<span class="hljs-keyword">import</span> tornado.httpserver
<span class="hljs-keyword">import</span> tornado.options
<span class="hljs-keyword">from</span> tornado.options <span class="hljs-keyword">import</span> options, define
<span class="hljs-keyword">from</span> tornado.web <span class="hljs-keyword">import</span> RequestHandler

define(<span class="hljs-string">&quot;port&quot;</span>, default=<span class="hljs-number">8000</span>, type=int, help=<span class="hljs-string">&quot;run server on the given port.&quot;</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        self.write(<span class="hljs-string">&quot;hello itcast.&quot;</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SubjectCityHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, subject, city)</span>:</span>
        self.write((<span class="hljs-string">&quot;Subject: %s&lt;br/&gt;City: %s&quot;</span> % (subject, city)))

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SubjectDateHandler</span><span class="hljs-params">(RequestHandler)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, date, subject)</span>:</span>
        self.write((<span class="hljs-string">&quot;Date: %s&lt;br/&gt;Subject: %s&quot;</span> % (date, subject)))

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
    tornado.options.parse_command_line()
    app = tornado.web.Application([
        (<span class="hljs-string">r&quot;/&quot;</span>, IndexHandler),
        (<span class="hljs-string">r&quot;/sub-city/(.+)/([a-z]+)&quot;</span>, SubjectCityHandler), <span class="hljs-comment"># &#x65E0;&#x540D;&#x65B9;&#x5F0F;</span>
        (<span class="hljs-string">r&quot;/sub-date/(?P&lt;subject&gt;.+)/(?P&lt;date&gt;\d+)&quot;</span>, SubjectDateHandler), <span class="hljs-comment">#&#x3000;&#x547D;&#x540D;&#x65B9;&#x5F0F;</span>
    ])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()
</code></pre>
<p><img src="../images/re_group_seq.png" alt="&#x672A;&#x547D;&#x540D;&#xFF0C;&#x987A;&#x5E8F;&#x4F20;&#x9012;">
<img src="../images/re_group_name.png" alt="&#x547D;&#x540D;&#xFF0C;&#x6309;&#x540D;&#x4F20;&#x9012;"></p>
<p><strong>&#x5EFA;&#x8BAE;&#xFF1A;&#x63D0;&#x53D6;&#x591A;&#x4E2A;&#x503C;&#x65F6;&#x6700;&#x597D;&#x7528;&#x547D;&#x540D;&#x65B9;&#x5F0F;&#x3002;</strong></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../c03/s01.html" class="navigation navigation-prev " aria-label="Previous page: 3.1 Application"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../c03/s03.html" class="navigation navigation-next " aria-label="Next page: 3.3 输出"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
